package com.xj.edu.hotel.controller;

import com.xj.edu.hotel.domain.*;
import com.xj.edu.hotel.service.OrderService;
import com.xj.edu.hotel.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("userOrder")
public class UserOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomService roomService;

    /**
     *  用户订单列表
     */
    @RequestMapping("userorder")
    public String userorder( Model model, HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Order> orders = orderService.userorder(user.getUserId());
        model.addAttribute("orders",orders);
        return "userorder"; }

    /**
     * 订单详情
     */
    @RequestMapping("orderitem")
    public String orderitem(String id,Model model){
        Order order = orderService.orderitem(id);
        model.addAttribute("order",order);
        return "orderitem"; }
    /**
     *  订单支付页面
      */
    @RequestMapping("Payorder")
    public String Payorder(String id,Model model){
        Order order = orderService.orderitem(id);
        model.addAttribute("order",order);
        return "payorder";
    }
    /**
     * 订单支付（修改状态）
     */
    @RequestMapping("payOrder")
    public String payorder(String id,Model model){
        Order order = orderService.orderitem(id);
        HisOrder hisOrder = new HisOrder();
        hisOrder.setOrderId(order.getOrderId());
        hisOrder.setHotelId(order.getHotelId());
        hisOrder.setUserId(order.getUserId());
        hisOrder.setOrderDescription(order.getOrderDescription());
        hisOrder.setOrderStatus("已处理");
        hisOrder.setOrderTime(order.getOrderTime());
        hisOrder.setOrderInTime(order.getOrderInTime());
        hisOrder.setOrderPayMoney(order.getOrderPayMoney());
        hisOrder.setOrderPayStatus("已支付");
        orderService.insertHisOrder(hisOrder);
        orderService.delOrder(order.getOrderId());
        return "redirect:/userOrder/hisorder"; }
    /**
     * 取消订单
     */
    @RequestMapping("Hisorder")
    public String hisorder(String id,Model model){
        Order order = orderService.orderitem(id);
        Room room = roomService.findRoomByid(order.getRoomId());
        int roomCount = room.getRoomCount();
        room.setRoomCount(roomCount+1);
        roomService.updateRoomCount(room);

        HisOrder hisOrder = new HisOrder();
        hisOrder.setHotelId(order.getHotelId());
        hisOrder.setOrderDescription(order.getOrderDescription());
        hisOrder.setOrderTime(order.getOrderTime());
        hisOrder.setOrderInTime(order.getOrderInTime());
        hisOrder.setOrderPayMoney(order.getOrderPayMoney());
        hisOrder.setOrderPayStatus(order.getOrderPayStatus());
        hisOrder.setOrderStatus(order.getOrderStatus());
        hisOrder.setUserId(order.getUserId());
       // String orderId = order.getOrderId();
        orderService.insertHisOrder(hisOrder);
        orderService.delOrder(order.getOrderId());
        return "redirect:/userOrder/userorder"; }
    /**
     *  历史订单查看（取消的部分订单）
     */
    @RequestMapping("hisorder")
    public String findHisorder(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<HisOrder> hisorders = orderService.findHisorder(user.getUserId());
        model.addAttribute("hisorders",hisorders);
        return "hisorder"; }
    /**
     *  预定界面
     */
    @RequestMapping(value = "toorderinfo",method = RequestMethod.GET)
    public String toOrderinfo(String id,Model model){
        Room room = roomService.findRoomByid(id);
        model.addAttribute("room",room);
        return "orderinfo";
    }
    /**
     *  创建订单（酒店预订）
     */
    @RequestMapping("orderinfo")
    public String orderInfo(String orderInTime,String hotelId, String roomId, HttpSession session) throws ParseException {

        //通过房间id查询到要预定的房间，减少房间数量
        Room room = roomService.findRoomByid(roomId);
        int roomCount = room.getRoomCount();
        room.setRoomCount(roomCount-1);
        //房间数量减一，然后保存
        roomService.updateRoomCount(room);

        User user = (User) session.getAttribute("user");
        SimpleDateFormat formatDate = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss");
        Date currentTime =  new Date();
        String time = formatDate.format(currentTime);
        Date date = formatDate.parse(time);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date Intime = format.parse(orderInTime);

        Order order =new Order();
//        BigDecimal bigDecimalStr = new BigDecimal("200.00");
        order.setOrderInTime(Intime);
        order.setUserId(user.getUserId());
        order.setHotelId(hotelId);
        order.setRoomId(roomId);
        order.setOrderDescription("test");
        order.setOrderStatus("已预订");
//        order.setOrderPayMoney(bigDecimalStr);
        order.setOrderPayStatus("未支付");
        order.setOrderTime(date);

        orderService.insertOrder(order);
        return "redirect:/userOrder/userorder";
    }
    }
